#!/usr/bin/env/python3.7
# -*- coding: utf-8 -*-

"""
@File    : gpios.py
@Time    : 12/06/2021
@Author  : Silence
@Version : 1.0
@Contact : silence2021silence@163.com
@License : (C)Copyright 2021-2021, Silence
@Desc    : None
"""

import RPi.GPIO as GPIO
import time

class Button():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
    def click_init():
        result = 0  # 长按返回0，单击返回1，双击返回2
        for i in range(200):
            sig = GPIO.input(17)  # 输入的电平信号
            if sig == GPIO.HIGH and result == 0:  # 第一次发现没有在按着
                result = 1
            if result == 1 and sig == GPIO.LOW:
                result = 2
                return result
            if i >= 100 and result != 0:  # 如果不是长按，1秒内返回结果
                return result
            time.sleep(0.01)  # 每10s执行一次判断
        return result

    def click_return(init):
        if init == 'init':
            while True:
                if GPIO.input(17) == GPIO.LOW:
                    click_time = Button.click_init()
                    if click_time == 0:
                        print('你长按了')
                        return click_time
                    else:
                        print('你单击了%d下' % click_time)
                        return click_time
                time.sleep(0.1)

class Gpios():
    def led(bright,step):
        GPIO.setmode(GPIO.BCM)
        GPIO.setwarnings(False)
        GPIO.setup(4, GPIO.OUT)
        if bright == 'bright' and step == 0:
            GPIO.output(4, GPIO.HIGH)
        elif bright == 'bright':
            t = True
            while t:
                GPIO.output(4, GPIO.HIGH)
                time.sleep(1)
                GPIO.output(4, GPIO.LOW)
                time.sleep(1)
                step -= 1
                if step == 0:
                    t = False
        elif bright == 'unbright':
            GPIO.output(4, GPIO.LOW)

    def buzzer(beep, step):
        GPIO.setmode(GPIO.BCM)
        GPIO.setwarnings(False)
        GPIO.setup(18, GPIO.OUT)
        if beep == 'beep':
            t = True
            while t:
                GPIO.output(18, GPIO.HIGH)
                time.sleep(0.2)
                GPIO.output(18, GPIO.LOW)
                time.sleep(0.2)
                step -= 1
                if step == 0:
                    t = False
        if beep == 'unbeep':
            GPIO.output(18, GPIO.LOW)
